package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        // 1. 参数校验  (dto 是否为空   分页参数是否正确)
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        // 2. 封装查询条件
        // 2.1  分页条件
        Page pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 2.2  查询条件
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(dto.getName())) {
            wrapper.like(AdChannel::getName, dto.getName());
        }
        wrapper.orderByAsc(AdChannel::getOrd);
        // 3. 封装返回结果
        IPage pageResult = this.page(pageReq, wrapper);
        return new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        // 1. 校验参数
        if (channel == null || StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        if (channel.getName().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道字符不能超过10");
        }
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已经存在");
        }
        //2.保存频道
        channel.setCreatedTime(new Date());
        this.save(channel);
        // 3.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        //1.校验
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id不能为空");
        }
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST, "id不存在");
        }
        String inputName = adChannel.getName();
        if (StringUtils.isNotBlank(inputName) && !inputName.equals(channel.getName())) {
            int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, inputName));
            if (count > 0) { // 该频道已经存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已存在");
            }
        }
        // 2. 修改频道
        updateById(adChannel);
        // 3. 返回结果
        return ResponseResult.okResult();
    }
    @Transactional
    @Override
    public ResponseResult delete(Integer id) {
        // 1. 判断参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        // 2. 根据id查询频道信息
        AdChannel channel = getById(id);
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        // 3. 判断status是否为true   有效不能删除
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效无法删除");
        }
        // 4. 删除掉
        removeById(id);

        if(id==46){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id=46不让删除");
        }
        // 5. 返回结果
        return ResponseResult.okResult();
    }

}